using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.Security.Principal;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Config = Dimok.Config;
using Dimok.Data;
using Dimok.Data.Database;
using Dimok.BlackBook;

namespace Dimok.Aspx{
	public partial class TestForm : System.Web.UI.Page{
		protected HtmlGenericControl spnInclude;
	
		protected void Page_Load(object sender, System.EventArgs e){
			DateTime date = DateTime.Now;
			Manager.UOX uo = new Manager.UOX();
			spnInclude.InnerHtml = uo.UserPath;
			try{
				try{
					DirectoryEntry deGroup = null;
					//ADs.FindGroup("Web_AMFC_Allied_Admin");
					//ltr.Text += "\nGroup'''s RID:"+ADs.GetRID(deGroup);
					//ltr.Text += "\n\tprimaryGroupID:"+deGroup.Properties["primaryGroupID"].Value;
					//ltr.Text += "\n\tFound by PGID:"+ADs.FindUsersByProperty("primaryGroupID",ADs.GetRID(deGroup))[0].Path;
					if( Manager.UOX.GetQueryParam("GroupUsers") != "" ){
						foreach( object de in ADs.LDAP.GetGroupUsers(ADs.LDAP.FindGroup(uo.GroupsOU,Request["GroupUsers"]).Path) )
							ltr.Text += "\n\t"+de.GetType().FullName;
						//ltrUsers.Text += ADs.ListGroupUsers(Request["GroupUsers"]).GetXml();
					}
					if( Manager.UOX.GetQueryParam("NewOU") != "" ){
						string ou = Manager.UOX.GetQueryParam("NewOU");
						//ltr.Text += uo.CreateCompany(ou,ou,"****").Path;
					}
					if( Request["findProp"] != null && Request["findProp"] != "" ){
						DirectoryEntry[] deUsers = ADs.LDAP.FindUsersByProperty(Request["findProp"],Request["findFilter"]);
						ltr.Text += "\ndeUsers.Length:"+deUsers.Length;
						foreach( DirectoryEntry de in deUsers )
							ltr.Text += "\n\t"+de.Properties["DisplayName"].Value;
					}
					if( ADs.isInDomain(uo.UserPath) ){
						ltr.Text += "\nIsInDomain:"+ADs.isInDomain(uo.UserPath);
						ltr.Text += "\nDomainName:"+ADs.LDAP.DomainName;
						ltr.Text += "\nGroupsDN:"+uo.GroupsDN;
						rptGroupsAllInNS.DataSource = ADs.LDAP.FindGroups(uo.GroupsDN);
						rptGroupsAllInNS.DataBind();
						rptUsersAllInNS.DataSource = uo.GetAllUsersInNamespace();
						rptUsersAllInNS.DataBind();
						//grdAccountExecs.DataSource = uo.AccountExecs;
						//grdAccountExecs.DataBind();
					}
					if( Manager.UOX.GetQueryParam("GroupMembers") != "" ){
						string groupDN = "CN="+Manager.UOX.GetQueryParam("GroupMembers")+","+Config.ADS.AppGroupsDN;
						deGroup = new DirectoryEntry(groupDN);
						ltr.Text += "\n"+Manager.UOX.GetQueryParam("GroupMembers")+"(Path:"+deGroup.Path+") members:";
						DirectoryEntry[] users = ADs.LDAP.GetGroupUsers(groupDN);
						foreach( DirectoryEntry user in users)
							ltr.Text += "\n\t"+user.Properties["DisplayName"].Value+" Ext:"+ADs.TelephoneExtention(user);
						foreach( string propName in users[0].Properties.PropertyNames )
							try{
								ltr.Text += "\n\t"+propName+":["+users[0].Properties[propName].Value+"]";
							}catch{}
						HtmlSelect selMembers = new HtmlSelect();
						ltr.Parent.Controls.AddAt(0,selMembers);
						DataSet dsMembers = ADs.LDAP.ListGroupUsers(groupDN);
						dsMembers.Tables[0].Columns.Add("Desc",typeof(string),"Name+' Ext:'+Ext");
						selMembers.DataSource = dsMembers;
						selMembers.DataTextField = "Desc";
						selMembers.DataValueField = "Ext";
						selMembers.DataBind();
					}
					if( Context.Request["prop"] != null )ltr.Text += "\n"+Context.Request["prop"]+":"+uo.Get(Context.Request["prop"]);
					ltr.Text += "\nNameSpaceOU:\n\t"+uo.NameSpaceOU.Path;
					ltr.Text += "\nGroups:\n\t"+string.Join("\n\t",uo.Groups);
					if( ADs.isInDomain(uo.UserPath) )
						ltr.Text += "\nPrimeGroup:";ltr.Text += Dimok.Data.ADs.LDAP.GetUserPrimaryGroup(uo.UserPath).Path;
					ltrMenu.Text = SQLHelperEx.XmlInnerText(uo.db,"SELECT * FROM dbo.fs_MenuID(DEFAULT,DEFAULT)");
					//ltrApplicants.Text = SQL.XmlInnerText(uo.db,"SELECT ApplicantID FROM dbo.fs_ApplicantsExx(DEFAULT,78)");
					//ltrApplicants.Text = SQLX.XmlInnerText(uo.db,"SELECT ApplicantID FROM vsec_List_Underwrite");
				}catch(Exception exp){
					ltr.Text += "\n************************************\n"+ShowException(exp);
				}finally{
					ltr.Text += "\nNameSpace:"+uo.NameSpace;
					ltr.Text += "\nWindowsIdentity:"+WindowsIdentity.GetCurrent().Name;
					ltr.Text += "\nUserName:"+uo.UserPath;
					ltr.Text += "\ndb:"+SQLHelperEx.GetSafeConnString(uo.db);
					ltr.Text += "\ndbAdo:"+SQLHelperEx.GetSafeConnString(uo.dbADO);
					ltr.Text += "\n"+DateTime.Now;
					if(uo.hasApplID)ltr.Text += "\nApplID:"+uo.applID;
				}
			}catch(Exception exc){ShowException(exc); }
		}
		
		private string ShowException(Exception exc){
			return (exc.InnerException!=null ? ShowException(exc.InnerException) : "") + "<pre>\nException:"+exc.GetType().FullName+"\n***Message: "+exc.Message+"\nTargetSite: "+exc.TargetSite+"\nSource: "+exc.Source+"\nStackTrace:\n"+exc.StackTrace+"\n</pre>";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
